﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Forget.HRBLL.Logical;
using Forget.HRBLL.View;

namespace HRWebSite.Sys
{
    public partial class ModifyUserAuthority : BasePage
    {
        protected override void Page_Load(object sender, EventArgs e)
        {
            this.HRLimits = Limits.LimitManage;
            this.HRLimitAttribute = Forget.HRBLL.LimitAttribute.Read | Forget.HRBLL.LimitAttribute.Add | Forget.HRBLL.LimitAttribute.Modify;
            base.Page_Load(sender, e);
            if (!this.IsPostBack)
                this.BindGridView();
        }

        private void BindGridView()
        {
            Forget.HRBLL.Model.User user;
            if (!string.IsNullOrEmpty(this.Request.Params["NumID"]) && (user = UserLogical.GetUserInfo_NumberID(this.Request.Params["NumID"])) != null)
            {
                ViewState["__NumberID__EditUserAuthority"] = user.NumberID;
                this.GridView1.Caption = string.Format("用户姓名：{0}", user.Name);
                List<LimitView> views = UserLimitLogical.GetUserOperateLimit(user.NumberID);
                if (views != null)
                {
                    this.GridView1.DataSource = InnerLimint.FromLimitView(views).OrderBy(x => x.EnID);
                    this.GridView1.DataBind();
                    foreach (GridViewRow row in this.GridView1.Rows)
                    {
                        foreach (TableCell cell in row.Cells)
                        {
                            foreach (Control con in cell.Controls)
                            {
                                if (con is CheckBox)
                                    ((CheckBox)con).Enabled = true;
                            }
                        }
                    }
                }
            }
        }

        

        protected void Button1_Click(object sender, EventArgs e)
        {
            string numberID = (string)ViewState["__NumberID__EditUserAuthority"];
            if (!string.IsNullOrEmpty(numberID))
            {
                List<LimitView> views = new List<LimitView>();
                foreach (GridViewRow row in this.GridView1.Rows)
                {
                    LimitView view = new LimitView() { NumberID = numberID, Attributes = 0 };
                    for (int i = 0; i < row.Cells.Count; i++)
                    {
                        if (i == 0)
                            view.EnID = row.Cells[0].Text;
                        else if (row.Cells[i].Controls[0] is CheckBox)
                        {
                            if (((CheckBox)row.Cells[i].Controls[0]).Checked)
                                view.Attributes = view.Attributes | (long)Math.Pow(2, i - 1);
                        }
                    }
                    views.Add(view);
                }
                UserLimitLogical.ModifyUserOperateLimit(views);
                Response.Write("<script>alert('修改成功');</script>");
            }
            else
                Response.Write("<script>alert('请选择用户');</script>");
        }
    }
}